package gui;

import sistema.Sistema;


import com.trolltech.qt.gui.QDialog;
import com.trolltech.qt.gui.QHBoxLayout;
import com.trolltech.qt.gui.QIcon;
import com.trolltech.qt.gui.QLabel;
import com.trolltech.qt.gui.QLineEdit;
import com.trolltech.qt.gui.QMessageBox;
import com.trolltech.qt.gui.QPushButton;
import com.trolltech.qt.gui.QTextEdit;
import com.trolltech.qt.gui.QVBoxLayout;
import com.trolltech.qt.gui.QWidget;
import com.trolltech.qt.gui.QLineEdit.EchoMode;

import excecoes.ContaException;
import excecoes.IPException;
import excecoes.PessoaException;
import excecoes.SistemaException;
import excecoes.UsuarioException;

public class NewAccountWindow extends QDialog {
	
	private final int MIN_PASSWORD_LENGTH = 6;
	private final String IMG_PATH = "ImHereM03/img/";
	private QVBoxLayout layout;
	private QLineEdit nameField, usernameField, emailField, passwordField,
			confirmField, phoneField;
	private QPushButton cancelButton;
	private QPushButton okButton;
	private Sistema sistema;

	public NewAccountWindow(Sistema sistema) {
		this.setWindowTitle("Nova Conta");
		this.setFixedSize(300, 320);
		this.setStyleSheet("background: url("+IMG_PATH + "background.png);");

		this.nameField = new QLineEdit();
		this.usernameField = new QLineEdit();	
		this.emailField = new QLineEdit();
		this.passwordField = new QLineEdit();
		this.passwordField.setEchoMode(EchoMode.Password);
		this.confirmField = new QLineEdit();
		this.confirmField.setEchoMode(EchoMode.Password);
		this.phoneField = new QLineEdit();
		this.sistema = sistema;

		Object[][] elements = { { this.nameField, "Nome" },
				{ this.usernameField, "Username" },
				{ this.emailField, "Email" },
				{ this.passwordField, "Senha" },
				{ this.confirmField, "Confirmar Senha" },
				{ this.phoneField, "Telefone" } };

		this.layout = new QVBoxLayout();

		for (int i = 0; i < elements.length; i++) {
			QHBoxLayout subLayout = new QHBoxLayout();

			QLabel description = new QLabel((String) elements[i][1]);

			subLayout.addWidget(description);
			subLayout.addWidget((QWidget) elements[i][0]);

			this.layout.addLayout(subLayout);
			this.layout.addSpacing(10);
		}

		
		this.layout.addSpacing(50);
		this.layout.addLayout(createFinalizeOptions());
		this.layout.addStretch(4);

		this.setLayout(this.layout);

		this.okButton.clicked.connect(this, "createAccount()");
		this.cancelButton.clicked.connect(this, "close()");
		
		this.setWindowIcon(new QIcon(IMG_PATH + "icon.png"));
	}

	private QHBoxLayout createFinalizeOptions() {
		QHBoxLayout layout = new QHBoxLayout();

		this.cancelButton = new QPushButton("Cancel");
		this.okButton = new QPushButton("Ok");

		layout.addWidget(this.cancelButton);
		layout.addSpacing(10);
		layout.addWidget(this.okButton);

		return layout;

	}

	@SuppressWarnings("unused")
	private void createAccount() throws ContaException, UsuarioException,
			PessoaException {
		
		if (nameField.text().equals("")){
			nameField.setStyleSheet("QLineEdit { background-color: red;} ");
		} else {
			nameField.setStyleSheet("QLineEdit { background-color: white;}");
			if (emailField.text().equals("")){
				emailField.setStyleSheet("QLineEdit { background-color: red;}");
			} else {
				emailField.setStyleSheet("QLineEdit { background-color: white;}");
				if (passwordField.text().equals("") || passwordField.text().length() < MIN_PASSWORD_LENGTH){
					passwordField.setStyleSheet("QLineEdit { background-color: red;}");
				} else {
					passwordField.setStyleSheet("QLineEdit { background-color: white;}");
					if (!(this.passwordField.text().equals(this.confirmField.text()))) {
						this.passwordField
								.setStyleSheet("QLineEdit { background-color: red;} ");
						this.confirmField
								.setStyleSheet("QLineEdit { background-color: red;} ");
						QMessageBox.critical(this, "Fatal",
								"As senhas não conferem.");
						return;
					} else {
						this.passwordField
						.setStyleSheet("QLineEdit { background-color: white;} ");
						this.confirmField
						.setStyleSheet("QLineEdit { background-color: white;} ");
						if (passwordField.text().length() < MIN_PASSWORD_LENGTH){
							passwordField.setStyleSheet("QLineEdit { background-color: red;}");
						} else {
							passwordField.setStyleSheet("QLineEdit { background-color: white;}");
							if (usernameField.text().equals("")){
								usernameField.setStyleSheet("QLineEdit { background-color: red;}");
							} else {
								usernameField.setStyleSheet("QLineEdit { background-color: white;}");
							}
						}
					}
				}
			}
			
			
		}

		try {
			sistema.criarUsuario(usernameField.text(), nameField.text(), emailField
					.text(), this.passwordField.text(), phoneField.text());
			QMessageBox.information(this, "Conta Criada", "Novo usuario registrado.");
			this.close();
			if (!phoneField.text().isEmpty()) {
				String userName = sistema.login(usernameField.text(),
						passwordField.text(), "127.0.0.1");
				LoggedWindow logged = new LoggedWindow(userName, sistema);
				logged.show();
			}
		}

		catch (PessoaException e) {
			QMessageBox.critical(this, "Erro", e.getMessage());		
			
		} catch (SistemaException e) {
			QMessageBox.critical(this, "Erro", e.getMessage());
		} catch (IPException e) {
			QMessageBox.critical(this, "Erro", e.getMessage());
		}
		  catch (UsuarioException e) {
			QMessageBox.critical(this, "Erro", e.getMessage());
		} catch (NullPointerException e){
			sistema.criarUsuario(usernameField.text(), nameField.text(), emailField
					.text(), this.passwordField.text(), phoneField.text(), true);
			QMessageBox.information(this, "Conta Criada", "Novo usuario registrado.");
			this.close();
		}

	}

}
